package com.jams.goodox.widget;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jams.goodox.R;
import com.jams.library.widget.ProgressWheel;


/**
 * @author Cosmo
 * @Email cosmos1989@live.com
 * @date 2014-4-10 下午5:21:15
 */
public class LoadView {
    public NoDateClickListener noDateClickListener;

    public NoDateClickListener getNoDateClickListener() {
        return noDateClickListener;
    }


    public void setNoDateClickListener(NoDateClickListener noDateClickListener) {
        this.noDateClickListener = noDateClickListener;
    }

    public ReloadListener listener;

    public ReloadListener getListener() {
        return listener;
    }


    public void setReloadListener(ReloadListener listener) {
        this.listener = listener;
    }

    private RelativeLayout load_parent;
    public LinearLayout error;
    private RelativeLayout load;
    public LinearLayout on_nodate;
    public TextView on_nodate_text;
    ProgressWheel progress_wheel;
    private ImageView no_concern;


    public LoadView(View context) {
        try {
            error = (LinearLayout) context.findViewById(R.id.on_error);
            load = (RelativeLayout) context.findViewById(R.id.on_progress);
            on_nodate = (LinearLayout) context.findViewById(R.id.on_nodate);
            load_parent = (RelativeLayout) context.findViewById(R.id.load_parent);
            on_nodate_text = (TextView) context.findViewById(R.id.on_nodate_text);
            no_concern = (ImageView) context.findViewById(R.id.no_concern);
            progress_wheel = (ProgressWheel) context.findViewById(R.id.progress_wheel);
            if (error != null) {
                error.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.reload();
                        }

                    }
                });
            }

            if (on_nodate != null) {
                on_nodate.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (noDateClickListener != null) {
                            noDateClickListener.noDateClick();
                        }


                    }
                });
            }


            load_parent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public LoadView(Context context) {
        try {
            error = (LinearLayout) ((Activity) context).findViewById(R.id.on_error);
            load = (RelativeLayout) ((Activity) context).findViewById(R.id.on_progress);
            on_nodate = (LinearLayout) ((Activity) context).findViewById(R.id.on_nodate);
            load_parent = (RelativeLayout) ((Activity) context).findViewById(R.id.load_parent);
            on_nodate_text = (TextView) ((Activity) context).findViewById(R.id.on_nodate_text);
            no_concern = (ImageView) ((Activity) context).findViewById(R.id.no_concern);

            if (error != null) {
                error.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener == null) {
                            return;
                        }
                        listener.reload();
                    }
                });
            }
            if (on_nodate != null) {
                on_nodate.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (noDateClickListener != null) {
                            noDateClickListener.noDateClick();
                        }
                    }
                });
            }

            if (load_parent != null)
                load_parent.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void onError() {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.VISIBLE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.GONE);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }
        }
    }

    public void onLoad() {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.VISIBLE);
            on_nodate.setVisibility(View.GONE);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }

        }
    }

    public void onloadFinish() {

        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.GONE);
        }
        if (load_parent != null) {
            load_parent.setVisibility(View.GONE);
        }

    }

    public void onNoDate(String text) {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.VISIBLE);
            on_nodate_text.setText(text);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }
        }

    }


    public void onNoDate(String text, int resId) {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.VISIBLE);
            no_concern.setImageResource(resId);
            on_nodate_text.setText(text);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }
        }

    }

    public void onNoDate(int text, int resId) {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.VISIBLE);
            no_concern.setImageResource(resId);
            on_nodate_text.setText(text);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }
        }

    }

    public void onNoDate(int text) {
        if (error != null && load != null && on_nodate != null) {
            error.setVisibility(View.GONE);
            load.setVisibility(View.GONE);
            on_nodate.setVisibility(View.VISIBLE);
            on_nodate_text.setText(text);
            if (load_parent != null) {
                load_parent.setVisibility(View.VISIBLE);
            }
        }

    }


    public interface ReloadListener {
        void reload();
    }

    public interface NoDateClickListener {
        void noDateClick();
    }
}

